package com.tuhu.dai.infrastructure.adapter;

import com.google.common.cache.Cache;
import com.tuhu.dai.domain.auth.adapter.port.ILoginPort;
import com.tuhu.dai.infrastructure.gateway.IWeixinApiService;
import com.tuhu.dai.infrastructure.gateway.dto.WeixinQrCodeRequestDTO;
import com.tuhu.dai.infrastructure.gateway.dto.WeixinQrCodeResponseDTO;
import com.tuhu.dai.infrastructure.gateway.dto.WeixinTemplateMessageDTO;
import com.tuhu.dai.infrastructure.gateway.dto.WeixinTokenResponseDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import retrofit2.Call;
import retrofit2.Response;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: DaiTao
 * @description:
 * @create: 2025-08-31 14:35
 **/
@Service
public class ILoginPortAdapter implements ILoginPort {

    @Value("${weixin.config.appid}")
    private String appid;
    @Value("${weixin.config.secret}")
    private String secret;
    @Value("${weixin.config.template_id}")
    private String template_id;

    @Value("${weixin.config.grantType:client_credential}")
    private String grantType;

    @Resource
    private IWeixinApiService weixinApiService;

    @Resource
    private Cache<String, String> weixinAccessToken;

    @Override
    public String createQrCodeTicket() throws IOException {
        // 1.获取token
        String accessToken = weixinAccessToken.getIfPresent(appid);
        if(StringUtils.isBlank(accessToken)){
            Call<WeixinTokenResponseDTO> token = weixinApiService.getToken(grantType, appid, secret);


            Response<WeixinTokenResponseDTO> execute = token.execute();
            WeixinTokenResponseDTO body = execute.body();
            accessToken = body.getAccess_token();

            // 放入缓存
            weixinAccessToken.put(appid, accessToken);
        }


        // 2.获取二维码
        WeixinQrCodeRequestDTO weixinQrCodeReq = new WeixinQrCodeRequestDTO();
        weixinQrCodeReq.setExpire_seconds(2592000);
        weixinQrCodeReq.setAction_name(WeixinQrCodeRequestDTO.ActionNameTypeVO.QR_SCENE.getCode());
        weixinQrCodeReq.setAction_info(WeixinQrCodeRequestDTO.ActionInfo.builder()
                .scene(WeixinQrCodeRequestDTO.ActionInfo.Scene.builder().scene_id(100601).build()).build());
        Call<WeixinQrCodeResponseDTO> qrCode = weixinApiService.createQrCode(accessToken, weixinQrCodeReq);
        WeixinQrCodeResponseDTO weixinQrCodeRes = qrCode.execute().body();
        assert null != weixinQrCodeRes;
        return weixinQrCodeRes.getTicket();
    }

    @Override
    public void sendLoginTemplate(String openid) throws IOException {
        // 发送模版消息
        String accessToken = weixinAccessToken.getIfPresent(appid);
        if(StringUtils.isBlank(accessToken)){
            Call<WeixinTokenResponseDTO> token = weixinApiService.getToken(grantType, appid, secret);


            Response<WeixinTokenResponseDTO> execute = token.execute();
            WeixinTokenResponseDTO body = execute.body();
            accessToken = body.getAccess_token();

            // 放入缓存
            weixinAccessToken.put(appid, accessToken);
        }

        // 2. 发送模板消息
        Map<String, Map<String, String>> data = new HashMap<>();
        WeixinTemplateMessageDTO.put(data, WeixinTemplateMessageDTO.TemplateKey.USER, openid);

        WeixinTemplateMessageDTO templateMessageDTO = new WeixinTemplateMessageDTO(openid, template_id);
        templateMessageDTO.setUrl("https://gaga.plus");
        templateMessageDTO.setData(data);

        Call<Void> call = weixinApiService.sendMessage(accessToken, templateMessageDTO);
        call.execute();
    }
}
